package com.desmart.renjiao.orgainzation;

import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.desmart.renjiao.R;
import com.desmart.renjiao.bean.MemberInfo;
import com.desmart.renjiao.contants.Constants;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.utils.PinyUtils;

import java.util.List;

public class MemberItemAdapter extends BaseQuickAdapter<MemberInfo,MemberItemAdapter.MemberItemViewHolder>{

    private int[] colors = new int[]{R.color.colorAccent,R.color.text_red,R.color.text_gray};

    public MemberItemAdapter(@Nullable List<MemberInfo> data) {
        super(R.layout.list_item_member_data, data);
    }

    @Override
    protected void convert(MemberItemViewHolder helper, MemberInfo item) {
        helper.tvTag.setText(PinyUtils.getAlpha(item.getUsername()));
        helper.tvTag.setVisibility(showKey(helper.getAdapterPosition()) ? View.VISIBLE : View.GONE);
        helper.tvUserName.setText(item.getUsername() + "(" + item.getSapsystemid() + ")");
        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(ARoutePath.addresslist.MemberInfoActivity)
                        .withSerializable(Constants.Memember.Key_MemberInfo,item)
                        .navigation(mContext);
            }
        });
        helper.tvHead.setBackgroundColor(mContext.getResources().getColor(colors[helper.getAdapterPosition() % 3]));
        helper.tvHead.setText(item.getUsername().substring(0, 1));
    }

    private boolean showKey(int pos) {
        if (pos == 0) {
            return true;
        }
        String a1 = PinyUtils.getAlpha(mData.get(pos).getUsername());
        String a0 = PinyUtils.getAlpha(mData.get(pos - 1).getUsername());
        return !a1.equals(a0);
    }

    class MemberItemViewHolder extends BaseViewHolder{

        private final TextView tvUserName;
        private final TextView tvTag;
        private final TextView tvHead;

        public MemberItemViewHolder(View view) {
            super(view);
            tvUserName = view.findViewById(R.id.tv_username);
            tvTag = view.findViewById(R.id.tv_tag);
            tvHead = view.findViewById(R.id.tv_head);
        }
    }
}
